//
//  YTStudyHeaderView.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/11/13.
//

#import "YTStudyHeaderView.h"

@interface YTStudyHeaderView ()
@property (nonatomic ,strong)  UIView * backview;
@end

@implementation YTStudyHeaderView

+ (instancetype)initview{
    YTStudyHeaderView * view = [YTStudyHeaderView new];
    view.clipsToBounds = YES;
    view.backgroundColor = UIColor.clearColor;
    [view newUI];
    return view;
}
- (void)newUI{
    
    UIView * backview = [UIView new];
    backview.backgroundColor = UIColor.whiteColor;
    self.backview = backview;
    [DTKTool YJ:kwt(5) andView:backview];
    [self addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(kwt(8));
        make.right.offset(kwt(-8));
    }];
    
    UILabel * tjlab = [DTKTool labelWithText:@"学习推荐" andFont:kCFont(18) andTextColor:KcolorHex(@"#222222")];
    [self addSubview:tjlab];
    [tjlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(8));
        make.top.equalTo(backview.mas_bottom).offset(kwt(18));
        make.height.offset(kwt(18));
    }];
}

- (void)setBTModelArray:(NSMutableArray<YTStashOneModel *> *)BTModelArray{
    _BTModelArray = BTModelArray;
    
    self.backview.hidden = (BTModelArray.count < 1);
    MJWeakSelf
    [self.backview.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat X = 0;
    CGFloat Y = 0;
    CGFloat W = (KSCREEN_WIDTH - kwt(16))/5.0;
    for (YTStashOneModel * model in BTModelArray) {
        
        UIView * btnview = [UIView new];
        [self.backview addSubview:btnview];
        [btnview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(Y);
            make.left.offset(X);
            make.width.offset(W);
            make.height.offset(kwt(75));
        }];
        
        btnview.addClickBlock(^{
            if(weakSelf.openBlock){
                weakSelf.openBlock(model);
            }
        });
        UIImageView * icon = kimageView(@"tangkou_photo");
        [icon LZsetImageWithImagePath:model.icon placeholderImage:[UIImage imageNamed:@"tangkou_photo"]];
        [btnview addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(kwt(12));
            make.centerX.offset(0);
            make.height.width.offset(kwt(30));
        }];
        
        UILabel * lab = [DTKTool labelWithText:model.typeName andFont:kFont(13) andTextColor:KcolorHex(@"#424242")];
        lab.textAlignment = NSTextAlignmentCenter;
        [btnview addSubview:lab];
        [lab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(kwt(54));
            make.left.right.centerX.offset(0);
        }];
        X += W;
        if(X >= 5 * W){
            X = 0;
            Y += kwt(75);
        }
        if(model == BTModelArray.lastObject){
            [btnview mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.offset(0);
            }];

        }
    }
    
    
}

@end
